//
//  HomeViewController.swift
//  WhyTest
//
//  Created by 宋广越 on 2022/2/7.
//

import UIKit

private let kTitleViewH : CGFloat = 40

class HomeViewController: UIViewController {
    //增加加载属性
    private lazy var pageTitleView : PageTitleView = {[weak self] in
        let titleFrame = CGRect(x: 0, y: kStatusBarH+kNavigationBarH, width:kScreenW,height:kTitleViewH)
        let titles = ["推荐","游戏","娱乐","趣玩"]
        let titleView = PageTitleView(frame: titleFrame, titles: titles)
        //我想成为了你的代理,我成为了你的代理
        titleView.delegate = self
        //titleView.backgroundColor = UIColor.orange
        return titleView
    }()

    private lazy var pageContentView : PageContentView = {[weak self] in
        //1,确定我们内容的frame
        let contentH = 750.0 - kStatusBarH - kStatusBarH - kNavigationBarH - kTitleViewH - kTableBarH
        let contentFrame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH + kTitleViewH, width: kScreenW, height: contentH)
        //identify all  child controllers
        var childVcs = [UIViewController]()
        childVcs.append(RecommendViewController())
        for _ in 0..<3 {
            let vc  = UIViewController()
            vc.view.backgroundColor = UIColor(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
            childVcs.append(vc)
        }
        let contentView = PageContentView(frame: contentFrame, childVcs: childVcs, parentViewController: self)
        contentView.delegate = self
        return contentView
    }()
    
    //系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置ui界面
        setupUI()
    }
}

/// 设置ui界面
extension HomeViewController{
   private func setupUI(){
       //1,设置导航栏
       setupNavigationBar()
       //2,添加titleView
       view.addSubview(pageTitleView)
       //add pagecontent view
       view.addSubview(pageContentView)
       //pageContentView.backgroundColor = UIColor.purple
       
    }
    private func setupNavigationBar(){
        //设置左侧icon
        let btn = UIButton()
        btn.setImage(UIImage(named: "logo"), for: .normal)
        btn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(imageName: "logo")
        //设置右侧item
        let size = CGSize(width: 40, height: 40)
        let qrcodeItem = UIBarButtonItem.init(imageName: "qrcode", highImageName: "qrcode_high", size: size)
         let historyItem = UIBarButtonItem.init(imageName: "lishi", highImageName: "lishi_high", size: size)
         let searchItem = UIBarButtonItem.init(imageName: "sousuo", highImageName: "sousuo_high", size: size)        
        navigationItem.rightBarButtonItems = [qrcodeItem,searchItem,historyItem]
    }
}
//我成为了你的代理,我要遵守什么规则的,或者说,我用什么方式获得你的值
extension HomeViewController : PageTitleViewDelegate {
    //我租售了你的规则,通过这种方法拿到了你的值
    func pageTitleVeiw(titleView: PageTitleView, selectedIndex index: Int) {
        //传递值到content对外方法里
        pageContentView.setCurrentIndex(currentIndex: index)
        }
}
////我成为了你的代理,我要遵守什么规则的,或者说,我用什么方式获得你的值
extension HomeViewController : PageContentViewDelegate {
    func pageContentViewDelegate(pageContentView: PageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        //传递值给pagetitle
        pageTitleView.setTitleWithProgress(progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
        
    }
}








